package com.it.controller;

import com.it.pojo.ClShoppingCar;
import com.it.pojo.LinkFoodCart;
import com.it.service.ClShoppingCarService;
import com.it.utils.ResultData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.UUID;

/**
 * @Controller
 *   作用1： 将当前对象的创建交给spring管理
 *   作用2：作为控制层代码的标识
 */
@Controller
public class ClShoppingCarController {

    private static final Logger logger = LoggerFactory.getLogger(ClShoppingCarController.class);

	@Autowired	//自动注入(由spring创建当前对象并为属性赋值)
	private ClShoppingCarService clShoppingCarService;
	
	/** 1.查询所有购物车信息 */
	@RequestMapping("/clShoppingCarList")
	public String clShoppingCarList(Model model){
		//调用service层的findAll方法，查询所有的购物车信息
		List<ClShoppingCar> list = clShoppingCarService.findAll();
		
		//将所有购物车信息的list集合存入model中，带到JSP显示
		model.addAttribute("list", list);
		
		return "cl/shoppingCar/list";
	}

	/**
	 * 2.根据id删除购物车信息
	 */
	@RequestMapping("/clShoppingCarDelete")
	public String clShoppingCarDelete(String id){
		//调用deleteById方法, 根据id删除购物车
		clShoppingCarService.deleteById(id);
		
		return "redirect:/clShoppingCarList";
	}

    /** 3.添加购物车信息 */
    @RequestMapping("/clShoppingCarAdd")
    public String clShoppingCarAdd(ClShoppingCar clShoppingCar){
        //System.out.println(clShoppingCar);
        //1.调用ClShoppingCarService层的addClShoppingCar方法, 添加购物车信息
        clShoppingCarService.addClShoppingCar(clShoppingCar);

        return "redirect:/clShoppingCarList";
    }

    /** 3.1 添加购物车信息 */
    @RequestMapping("/createCartForCurrentUser")
    @ResponseBody
    public ResultData<ClShoppingCar> createCartForCurrentUser(HttpServletRequest request, HttpServletResponse response){
        HttpSession session = request.getSession();
        ResultData<ClShoppingCar> resultData = new ResultData<ClShoppingCar>();

        String loginUserId = (String) session.getAttribute("loginUserId");
        String loginUserName = (String) session.getAttribute("loginUserName");
        ClShoppingCar cart = new ClShoppingCar();
        /**
         * 有了就直接用
         */
        List<ClShoppingCar> carts = clShoppingCarService.findByUserId(loginUserId);
        if(carts != null && carts.size() > 0){
            cart = carts.get(0);
            resultData.setData(cart);
            resultData.setCode(200);
            resultData.setMsg("已有餐车！");
            resultData.setSuccess(true);
            session.setAttribute("cartId", cart.getId());


            if(cart.getLinkFoodCarts() != null && cart.getLinkFoodCarts().size() > 0){
                String foodIds = "";
                String linkFoodCartIds = "";
                for(LinkFoodCart link:cart.getLinkFoodCarts()){
                    foodIds += link.getFoodId() + ", ";
                    linkFoodCartIds += link.getId() + ", ";
                }
                session.setAttribute("foodIds", foodIds);
                session.setAttribute("linkFoodCartIds", linkFoodCartIds);
            }
        }else{
            /**
             * 没有就新增
             */
            cart.setId(UUID.randomUUID().toString().substring(0, 32));
            cart.setUserId(loginUserId);
            cart.setUserName(loginUserName);
            int i = clShoppingCarService.addClShoppingCar(cart);
            if(i > 0){
                resultData.setData(cart);
                resultData.setCode(200);
                resultData.setMsg("新增餐车成功！");
                resultData.setSuccess(true);
                session.setAttribute("cartId", cart.getId());
            }else{
                resultData.setCode(500);
                resultData.setMsg("添加失败！");
                resultData.setSuccess(false);
            }
        }
        return resultData;
    }
	
	/**
	 * 4.根据id修改购物车信息
	 */
	@RequestMapping("/clShoppingCarInfo")
	public String clShoppingCarInfo(String id, Model model){
		//1.根据id查询购物车信息
		ClShoppingCar clShoppingCar = clShoppingCarService.findById(id);
		System.out.println(clShoppingCar);
		
		//2.将购物车信息存入model中(存入request域中)
		model.addAttribute("shoppingCar", clShoppingCar);
		//3.转向clShoppingCar_update.jsp
		return "cl/shoppingCar/update";
	}

	/**
	 * 5.根据id修改购物车信息
	 */
	@RequestMapping("/clShoppingCarUpdate")
	public String clShoppingCarUpdate(HttpServletRequest request, HttpServletResponse response, ClShoppingCar clShoppingCar){
		//System.out.println(clShoppingCar);
		//1.调用clShoppingCarService的updateById方法修改购物车信息
		clShoppingCarService.updateById(clShoppingCar);
		//2.转向查询所有购物车的方法
		return "redirect:/clShoppingCarList";
	}

}
